package com.crm.system.service.impl.activityManager;

import com.crm.model.entity.activityManager.CustomerGiftActivityRelation;
import com.crm.model.entity.productManager.CustomerProduct;
import com.crm.service.activityManager.CustomerGiftActivityRelationDbService;
import com.crm.service.productManager.CustomerProductDbService;
import com.crm.system.service.activityManager.CustomerGiftActivityRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @author luojie
 * @date 2021/3/1 14:45
 */
@Service
public class CustomerGiftActivityRelationServiceImpl implements CustomerGiftActivityRelationService {

    @Autowired
    private CustomerGiftActivityRelationDbService customerGiftActivityRelationDbService;
    @Autowired
    private CustomerProductDbService customerProductDbService;

    @Override
    public List<HashMap> selectGiftDropDownCheckedListByGiftActivityId(String giftActivityId) {
        List<HashMap> hashMapList = new ArrayList<HashMap>();
        //全部产品列表
        List<CustomerProduct> customerProductList = customerProductDbService.selectProductDropDownList("");
        //选中的赠品列表
        List<CustomerGiftActivityRelation> customerGiftActivityRelationList =  customerGiftActivityRelationDbService.selectGiftDropDownCheckedListByGiftActivityId(giftActivityId);
        customerProductList.stream().forEach(product -> {
            //如果赠品id和产品id相同，则checked标记成true，不相同则标记成false
            if(!customerGiftActivityRelationList.stream().anyMatch(giftActivityRelation -> giftActivityRelation.getCustomerProductId().equals(product.getCustomerProductId()))){
                HashMap hashMap = new HashMap();
                hashMap.put("productId",product.getCustomerProductId());
                hashMap.put("productName",product.getProductName());
                hashMap.put("checked",false);
                hashMapList.add(hashMap);
            }else{
                HashMap hashMap = new HashMap();
                hashMap.put("productId",product.getCustomerProductId());
                hashMap.put("productName",product.getProductName());
                hashMap.put("checked",true);
                hashMapList.add(hashMap);
            }
        });
        return hashMapList;
    }
}
